// d_init include server
#ifndef __XSETUP_INCL__
#define THIS_FILE "i_server.sqf"
#include "x_setup.sqf"
#endif
_bonus_ar_build = [];
#ifdef __DEFAULT__
GVAR(bap_counter) = 0;
_mpos = markerPos QGVAR(bonus_create_pos);
_mpos set [2,0];
GVAR(bonus_create_pos) = _mpos;
deleteMarker QGVAR(bonus_create_pos);
GVAR(bonus_air_positions) = [];
for "_i" from 1 to 10000 do {
	_mna = format [QGVAR(bonus_air_positions_%1), _i];
	_mpos = markerPos _mna;
	_mpos set [2,0];
	if (str _mpos == "[0,0,0]") exitWith {};
	GVAR(bonus_air_positions) set [count GVAR(bonus_air_positions), [_mpos, markerDir _mna]];
	// TYRGHEN NEARESTTAG
	//_nbb = _mpos nearestObject "Land_TentHangar_V1_F";
	_nbb = ["Land_TentHangar_V1_F",_mpos,25] call FUNC(nearestObjTypes);
	if (!isNull _nbb) then {
		_bonus_ar_build set [count _bonus_ar_build, _nbb];
		_nbb allowDamage false;
	};
	deleteMarker _mna;
};
#endif
#ifdef __TT__
_mpos = markerPos QGVAR(bonus_create_pos_w);
_mpos set [2,0];
GVAR(bonus_create_pos_w) = _mpos;
GVAR(bap_counter_w) = 0;
GVAR(bonus_air_positions_w) = [];
for "_i" from 1 to 10000 do {
	_mna = format [QGVAR(bonus_air_positions_w_%1), _i];
	_mpos = markerPos _mna;
	_mpos set [2,0];
	if (str _mpos == "[0,0,0]") exitWith {};
	GVAR(bonus_air_positions_w) set [count GVAR(bonus_air_positions_w), [_mpos, markerDir _mna]];
	// TYRGHEN NEARESTTAG
	// _nbb = _mpos nearestObject "Land_TentHangar_V1_F";
	_nbb = ["Land_TentHangar_V1_F",_mpos,25] call FUNC(nearestObjTypes);
	if (!isNull _nbb) then {
		_bonus_ar_build set [count _bonus_ar_build, _nbb];
		_nbb allowDamage false;
	};
	deleteMarker _mna;
};

_mpos = markerPos QGVAR(bonus_create_pos_e);
_mpos set [2,0];
GVAR(bonus_create_pos_e) = _mpos;
GVAR(bap_counter_e) = 0;
GVAR(bonus_air_positions_e) = [];
for "_i" from 1 to 10000 do {
	_mna = format [QGVAR(bonus_air_positions_e_%1), _i];
	if (markerType _mna != "") then {
		_mpos = markerPos _mna;
		_mpos set [2,0];
		if (str _mpos == "[0,0,0]") exitWith {};
		GVAR(bonus_air_positions_e) set [count GVAR(bonus_air_positions_e), [_mpos, markerDir _mna]];
		// TYRGHEN NEARESTTAG
		// _nbb = _mpos nearestObject "Land_TentHangar_V1_F";
		_nbb = ["Land_TentHangar_V1_F",_mpos,25] call FUNC(nearestObjTypes);
		if (!isNull _nbb) then {
			_bonus_ar_build set [count _bonus_ar_build, _nbb];
			_nbb allowDamage false;
		};
		deleteMarker _mna;
	};
};
#endif
[QGVAR(bonus_air_buildings),_bonus_ar_build] call FUNC(NetSetJIP);

#ifdef __DEFAULT__
GVAR(bvp_counter) = 0;
GVAR(bonus_vec_positions) = [];
for "_i" from 1 to 10000 do {
	_mna = format [QGVAR(bonus_vec_positions_%1), _i];
	if (markerType _mna != "") then {
		_mpos = markerPos _mna;
		_mpos set [2,0];
		if (str _mpos == "[0,0,0]") exitWith {};
		GVAR(bonus_vec_positions) set [count GVAR(bonus_vec_positions), [_mpos, markerDir _mna]];
		deleteMarker _mna;
	};
};
#endif
#ifdef __TT__
GVAR(bvp_counter_w) = 0;
GVAR(bonus_vec_positions_w) = [];
for "_i" from 1 to 10000 do {
	_mna = format [QGVAR(bonus_vec_positions_w_%1), _i];
	if (markerType _mna != "") then {
		_mpos = markerPos _mna;
		_mpos set [2,0];
		if (str _mpos == "[0,0,0]") exitWith {};
		GVAR(bonus_vec_positions_w) set [count GVAR(bonus_vec_positions_w), [_mpos, markerDir _mna]];
		deleteMarker _mna;
	};
};

GVAR(bvp_counter_e) = 0;
GVAR(bonus_vec_positions_e) = [];
for "_i" from 1 to 10000 do {
	_mna = format [QGVAR(bonus_vec_positions_e_%1), _i];
	if (markerType _mna != "") then {
		_mpos = markerPos _mna;
		_mpos set [2,0];
		if (str _mpos == "[0,0,0]") exitWith {};
		GVAR(bonus_vec_positions_e) set [count GVAR(bonus_vec_positions_e), [_mpos, markerDir _mna]];
		deleteMarker _mna;
	};
};
#endif

// add some random patrols on the island
// if the array is empty, no patrols
// simply place a rectangular marker called "d_isledefense_marker", marker text = number of patrols
if (GVAR(WithIsleDefense) == 0) then {
	_mname = QGVAR(isledefense_marker);
	_mpos = markerPos _mname;
	if (str _mpos == "[0,0,0]") exitWith {
		GVAR(with_isledefense) = [];
	};
	_mpos set [2,0];
	_mtext = markerText _mname;
	GVAR(with_isledefense) = [_mpos, (markerSize _mname) select 0, (markerSize _mname) select 1, markerDir _mname, parseNumber _mtext];
	deleteMarker _mname;
} else {
	GVAR(with_isledefense) = [];
};

__TRACE_1("","d_with_isledefense")

#ifndef __TT__
/*if (GVAR(MissionType) != 2) then {
	GVAR(fac_air_wreck) addEventHandler ["killed", {_this call FUNC(x_fackilled)}];
	GVAR(fac_air_chopper) addEventHandler ["killed", {_this call FUNC(x_fackilled)}];
	GVAR(fac_air_jet) addEventHandler ["killed", {_this call FUNC(x_fackilled)}];
};*/
if (GVAR(MissionType) != 2) then {
	0 spawn {
		scriptName "spawn_CreateWarFac";
		_wairfac = switch (GVAR(own_side)) do {
			case "BLUFOR": {GVAR(WarfareAircraftFactory_Blufor)};
			case "OPFOR": {GVAR(WarfareAircraftFactory_Opfor)};
			case "INDEPENDENT": {GVAR(WarfareAircraftFactory_Independent)};
		};
		if (count GVAR(aircraft_facs) > 0) then {
			_pos = (GVAR(aircraft_facs) select 0) select 0;
			_dir = (GVAR(aircraft_facs) select 0) select 1;
			[QGVAR(jet_serviceH),false] call FUNC(NetSetJIP);
			_fac = createVehicle [_wairfac, _pos, [], 0, "NONE"];
			_fac setDir _dir;
			_fac setPos _pos;
			_fac addEventHandler ["killed", {[_this select 0, 0] call FUNC(x_fackilled)}];
		};
		if (count GVAR(aircraft_facs) > 1) then {
			_pos = (GVAR(aircraft_facs) select 1) select 0;
			_dir = (GVAR(aircraft_facs) select 1) select 1;
			[QGVAR(chopper_serviceH),false] call FUNC(NetSetJIP);
			_fac = createVehicle [_wairfac, _pos, [], 0, "NONE"];
			_fac setDir _dir;
			_fac setPos _pos;
			_fac addEventHandler ["killed", {[_this select 0, 1] call FUNC(x_fackilled)}];
		};
		if (count GVAR(aircraft_facs) > 2) then {
			_pos = (GVAR(aircraft_facs) select 2) select 0;
			_dir = (GVAR(aircraft_facs) select 2) select 1;
			[QGVAR(wreck_repairH),false] call FUNC(NetSetJIP);
			_fac = createVehicle [_wairfac, _pos, [], 0, "NONE"];
			_fac setDir _dir;
			_fac setPos _pos;
			_fac addEventHandler ["killed", {[_this select 0, 2] call FUNC(x_fackilled)}];
		};
	};
} else {
	if (GVAR(MissionType) == 2) then {
		[QGVAR(jet_serviceH),false] call FUNC(NetSetJIP);
		[QGVAR(chopper_serviceH),false] call FUNC(NetSetJIP);
		[QGVAR(wreck_repairH),false] call FUNC(NetSetJIP);
	};
};
#endif

if (GVAR(with_ai)) then {
	_mpos = markerPos QGVAR(pos_aihut);
	_mpos set [2,0];
	GVAR(pos_ai_hut) = [_mpos, markerDir QGVAR(pos_aihut)];
	_wbarracks = switch (GVAR(own_side)) do {
		case "BLUFOR": {"Land_CashDesk_F"};
		case "OPFOR": {"Land_CashDesk_F"};
		case "INDEPENDENT": {"Land_CashDesk_F"};
	};

	_D_AI_HUT = createVehicle [_wbarracks, GVAR(pos_ai_hut) select 0, [], 0, "NONE"];
	_D_AI_HUT setDir (GVAR(pos_ai_hut) select 1);
	_D_AI_HUT setPos (GVAR(pos_ai_hut) select 0);
	_D_AI_HUT addEventHandler ["handleDamage", {0}];
	[QGVAR(AI_HUT), _D_AI_HUT] call FUNC(NetSetJIP);
};
deleteMarker QGVAR(pos_aihut);

#ifndef __TT__
GVAR(le_player_boxbos) = markerPos QGVAR(player_ammobox_pos);
GVAR(le_player_boxbos) set [2, 0];
if (isMultiplayer && isDedicated) then {
	deleteMarkerLocal QGVAR(player_ammobox_pos);
};
#else
GVAR(le_player_boxbos) = markerPos QGVAR(player_ammobox_pos);
GVAR(le_player_boxbos) set [2, 0];
GVAR(le_player_boxbose) = markerPos QGVAR(player_ammobox_pos_e);
GVAR(le_player_boxbose) set [2, 0];
if (isMultiplayer && isDedicated) then {
	deleteMarkerLocal QGVAR(player_ammobox_pos);
	deleteMarkerLocal QGVAR(player_ammobox_pos_e);
};
#endif

_civcenter = createCenter civilian;

#ifdef __OWN_SIDE_BLUFOR__
_opforcenter = createCenter opfor;
_independentcenter = createCenter independent;
blufor setFriend [opfor, 0.1];
opfor setFriend [blufor, 0.1];
blufor setFriend [independent, 1];
independent setFriend [blufor, 1];
opfor setFriend [independent, 0.1];
independent setFriend [opfor, 0.1];
#endif

#ifdef __OWN_SIDE_OPFOR__
_bluforcenter = createCenter blufor;
_independentcenter = createCenter independent;
blufor setFriend [opfor, 0.1];
opfor setFriend [blufor, 0.1];
blufor setFriend [independent, 0.1];
independent setFriend [blufor, 0.1];
opfor setFriend [independent, 1];
independent setFriend [opfor, 1];
#endif

#ifdef __OWN_SIDE_INDEPENDENT__
_bluforcenter = createCenter blufor;
_opforcenter = createCenter opfor;
blufor setFriend [opfor, 0.1];
opfor setFriend [blufor, 0.1];
independent setFriend [blufor, 0.1];
independent setFriend [opfor, 0.1];
blufor setFriend [independent, 0.1];
opfor setFriend [independent, 0.1];
#endif

#ifdef __TT__
_independentcenter = createCenter independent;
blufor setFriend [independent, 0.1];
independent setFriend [blufor, 0.1];
opfor setFriend [independent, 0.1];
independent setFriend [opfor, 0.1];
#endif
